<?php
#  视频认证订单支付


/**
 *支付成功后通知
 * @name store_notify.php
 * @package cwms
 * @category controller
 * @link /controller/notify.php
 * @author jianfang
 * @version 1.0
 * @since 2015-7-25
 */
/*
 appid:wx63a6e52d04f9ed63
bank_type:CMB_CREDIT 付款银行
cash_fee:1
fee_type:CNY
is_subscribe :Y 是否关注公众账号
mch_id:1247415201
nonce_str:bv4kv9du5y12olx9w3nff0yjvqfgpic0
openid:ob2eYuNyzq-AgR-vi3llKvIW2h1o
out_trade_no:hhffi8666
result_code:SUCCESS
return_code:SUCCESS
sign:14F44FEF0835B4257FBAC7D807AD06AE
time_end:20150726152935
total_fee:1
trade_type:JSAPI
transaction_id:1007880445201507260482512729
 */

$logPath = __DIR__ . '/../../../extend/pay/notify/logs/payment.log';

function write_file($filename, $text, $openmod = 'a')
{
    if (@$fp = fopen($filename, $openmod)) {
        flock($fp, 2);
        fwrite($fp, $text);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

$postStr = file_get_contents('php://input');
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$logPath = __DIR__ . '/../../../extend/pay/notify/logs/payment-vip-auth' . $postObj->out_trade_no . '.log';
write_file($logPath, "$postStr\r\n");

if ($postObj->return_code == "SUCCESS")
{
    if ($postObj->result_code == "SUCCESS")
    {
        $database = require __DIR__ . '/../../../application/database.php';

        $dbms = 'mysql';     //数据库类型
        $host = $database['hostname']; //数据库主机名
        $dbName = $database['database'];    //使用的数据库
        $user = $database['username'];      //数据库连接用户名
        $pass = $database['password'];          //对应的密码
        $dsn = "$dbms:host=$host;dbname=$dbName";
        $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象

        //得到订单号
        $rawOrderNum = $postObj->out_trade_no;
        $transaction_id = $postObj->transaction_id;
        $total_fee = $postObj->total_fee / 100;
        $host = $_SERVER['HTTP_HOST'];
        $filename = "https://{$host}/public/home/Pay/notifyByVipAuthOrder.html?rawOrderNum={$rawOrderNum}&total_fee={$total_fee}&transaction_id={$transaction_id}";
        write_file($logPath,$filename."\r\n");
        file_get_contents($filename);
        // 订单信息
//        $sql = <<< SQL
//SELECT a.id,a.code,a.type,a.user_id,b.amount,b.state
//FROM `hfztcy_order` AS a
//    INNER JOIN `hfztcy_order_vip_auth` AS b
//        ON `a`.`id` = `b`.`order_id`
//WHERE `a`.`code` = '{$rawOrderNum}'
//SQL;
//        $orderInfo = $dbh->query($sql)->fetch(2);
//        write_file($logPath, $sql."\r\n");
//
//        // 订单是否存在
//        if (empty($orderInfo)) {
//            write_file($logPath, "订单不存在\r\n");
//            return false;
//        }
//
//        // 判断订单状态
//        if ($orderInfo['state'] > 1 && !empty($orderInfo['transaction_id'])) {
//            write_file($logPath, '订单状态：' . $orderInfo['state'] . "\r\n");
//            return false;
//        }
//
//        $dbh->beginTransaction();
//        try {
//            // 更新订单
//            $add_time = date('Y-m-d H:i:s',time());
//            $updateOrderSql = <<< SQL
//UPDATE hfztcy_order
//SET pay_amount='{$total_fee}',pay_time = '{$add_time}',transaction_id='{$transaction_id}'
//WHERE `code` = '{$rawOrderNum}'
//SQL;
//            $dbh->exec($updateOrderSql);
//            write_file($logPath,'更新订单表：'.$updateOrderSql."\r\n");
//
//            $sql = <<< SQL
//UPDATE hfztcy_order_vip_auth
//SET state=1
//WHERE `order_id` = '{$orderInfo['id']}'
//SQL;
//            $dbh->exec($sql);
//            write_file($logPath,'更新订单表：'.$sql."\r\n");
//            $dbh->commit();
//        } catch (Exception $exception) {
//            $dbh->rollBack();
//        }
        // 分销
    }
}
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';die();